package com.skeleton.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

@Component
public class SystemInterceptor extends HandlerInterceptorAdapter {
	
	protected Logger logger = LogManager.getLogger(this.getClass());

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		// 当前请求路径
		String currentURI = request.getRequestURI().replace(request.getContextPath(), "").trim();
		// 不需要登陆的url

		logger.debug("================================");
		logger.debug("访问路径：" + currentURI);
		logger.debug("================================");
		logger.info("================================");
		logger.info("访问IP：" + request.getRemoteAddr());
		logger.info("================================");
		
		if(currentURI.startsWith("/admin/")) {
			String name = (String) request.getSession().getAttribute("user");
			if(name == null) {
				response.sendRedirect("../login");
				return false;
			}
		}
		
		return super.preHandle(request, response, handler);
	}

}
